/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package io;

import java.util.ArrayList;
import java.util.List;
import motej.Mote;
import motej.MoteFinder;
import motej.MoteFinderListener;
import tal.drivechain.chain.Chain;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.chain.ChainNode.MSG;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;
import tal.drivechain.variable.Variable;
import tal.drivechain.variable.types.DataType;

/**
 *
 * @author Tal Eisenberg
 */
public class WiiFinder extends Module implements MoteFinderListener {
    static List<Mote> wiiMotes = new ArrayList<Mote>();
    private Variable moteFoundMsg = new Variable("wiimoteFound", DataType.INT, -1);
    private MoteFinder finder;

    public WiiFinder() {
        super();
        finder = MoteFinder.getMoteFinder();
        finder.addMoteFinderListener(this);
    }

    @Override
    public void setParentChain(Chain parent) {
        if (this.getParentChain()!=null)
            this.getParentChain().getSharedObjects().remove("WiiFinder.wiiMotes");
        super.setParentChain(parent);
        parent.getSharedObjects().put("WiiFinder.wiiMotes", wiiMotes);
    }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
    }

    @Override
    public void start() {
        super.start();

        finder.startDiscovery();
        log("Discovering Wii remotes. Press the 1 and 2 buttons simultaneously to connect a wii remote.", MSG.ANOUNCE);
    }

    @Override
    public void stop() {
        super.stop();
        finder.stopDiscovery();
        finder.clearBluetoothCache();
        new Thread() {
            @Override
            public void run() {
                for (Mote m : wiiMotes) {
                    m.disconnect();
                    log("Wii mote " + wiiMotes.indexOf(m) + " (" + m.getBluetoothAddress() + ") has been disconnected.", MSG.ANOUNCE);
                }
                wiiMotes.clear();
            }
        }.start();
    }

    MoteFinder getFinder() {
        return finder;
    }

    private int wiimoteModuleCount() {
        int count = 0;
        for (ChainNode m : WiiFinder.this.getParentChain().getChildNodes().values()) {
            System.out.println(m.getName() + ": " + m.getNodeClassName());
            if (m.getNodeClassName().equals("io.WiiMote")) count++;
        }
        System.out.println("wiimoteModuleCount: " + count);
        return count;
    }

    public void moteFound(Mote mote) {
        wiiMotes.add(mote);
        log("Wiiremote " + (wiiMotes.size()-1) + " found at " + mote.getBluetoothAddress(), MSG.ANOUNCE);
        moteFoundMsg.setValue(wiiMotes.size()-1, true);
        WiiFinder.this.getParentChain().getVarTable().sendGlobalMessage(moteFoundMsg);
        if (wiiMotes.size()>=wiimoteModuleCount()) {
            finder.stopDiscovery();
            log("Stopping discovery.", MSG.ANOUNCE);
        }
        System.out.println("WIIMOTES:");
        for (int i=0; i<wiiMotes.size(); i++) System.out.println(i + ": " + wiiMotes.get(i).getBluetoothAddress());
        System.out.println("==========");
    }
}

